package com.neusoft.shopingapi.dao;

import com.neusoft.shopingapi.entity.CartEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CartDAOImpl {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    /**
     *查询购物车商品
     */
    public List<CartEntity> queryCart(Integer userno){
        String sql = "select * from cartinfo where userno=?";
        RowMapper<CartEntity> rowMapper=new BeanPropertyRowMapper<>(CartEntity.class);
        return jdbcTemplate.query(sql,rowMapper,userno);
    }
    /**
     *查询购物车是否有重复商品
     */
    public List<CartEntity> queryGoods(Integer userno,Integer goodsno){
        String sql = "select * from cartinfo where userno=? and goodsno=?";
        RowMapper<CartEntity> rowMapper=new BeanPropertyRowMapper<>(CartEntity.class);
        return jdbcTemplate.query(sql,rowMapper,userno,goodsno);
    }
    /**
     *添加购物车商品
     */
    public Integer addCart(CartEntity cartEntity){
        String sql = "insert into cartinfo values (null,?,?,?)";
        return jdbcTemplate.update(sql,cartEntity.getUserno(),cartEntity.getGoodsno(),cartEntity.getGoodsquantity());
    }
    /**
     *修改购物车商品数量
     */
    public Integer updateCart(CartEntity cartEntity){
        String sql = "update cartinfo set goodsquantity=? where cartno=?";
        return jdbcTemplate.update(sql,cartEntity.getGoodsquantity(),cartEntity.getCartno());
    }
    /**
     *删除购物车商品
     */
    public Integer delCart(Integer cartno){
        String sql="delete from cartinfo where cartno=?";
        return jdbcTemplate.update(sql,cartno);
    }
}
